package com.woniuxy.activiti.service;

import com.woniuxy.activiti.mapper.EnterpriseCreditMapper;
import com.woniuxy.entity.EnterpriseCredit;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xilei
 * @create 2021--15 13:09
 */
@Service
public class EnterpriseCreditService implements IActFlowCustomService {

    @Resource
    private EnterpriseCreditMapper enterpriseCreditMapper;

    /**
     * 查询企业授信列表
     * @param merchandiserId
     * @return
     */
    public List<EnterpriseCredit> findCreditsByUserId(int merchandiserId){
        return enterpriseCreditMapper.selectCreditsByUserId(merchandiserId);
    }

    /**
     * 查询一条授信信息详情
     * @param id
     * @return
     */

    public EnterpriseCredit findCreditById(int id){

        return enterpriseCreditMapper.selectCreditById(id);
    }

    public List<EnterpriseCredit> findCreditByEnterpriseId(int enterpriseId){

        return enterpriseCreditMapper.selectCreditsByEnterpriseId(enterpriseId);
    }

    /**
     * 设置流程变量
     * @param id
     * @return
     */
    @Override
    public Map<String, Object> setvariables(int id) {
        EnterpriseCredit enterpriseCredit = this.findCreditById(id);
        Map<String, Object> variables = new HashMap<>();
        variables.put("assignee0", 1);
        variables.put("assignee1", 2);
        variables.put("assignee2", 3);
        variables.put("enterpriseCredit",enterpriseCredit);
        return variables;
    }

    @Override
    public void startRunTask(int id) {
        enterpriseCreditMapper.startTask(id);
    }

    @Override
    public void endRunTask(int id) {
        enterpriseCreditMapper.endTask(id);
    }

    /**
     * 添加授信审批任务
     * @param enterpriseCredit
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int addEnterpriseCredit(EnterpriseCredit enterpriseCredit){
        int code = enterpriseCreditMapper.insertCredit(enterpriseCredit);
        return code;
    }

    public void updateCredit(boolean opinionResult,String opinionComment){
        enterpriseCreditMapper.updateCredit(opinionResult, opinionComment);
    }
}
